package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    private DeviceDataMapper deviceDataMapper;

    /**
     * 获取设备数据分页结果
     *
     * @param deviceDataPageReqDto
     * @return
     */
    @Override
    public PageResponse<DeviceDataVo> selectByPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(), deviceDataPageReqDto.getPageSize());

        Long startTime = deviceDataPageReqDto.getStartTime();
        Long endTime = deviceDataPageReqDto.getEndTime();

        deviceDataPageReqDto.setStartDate(ObjectUtil.isEmpty(startTime) ? null : LocalDateTimeUtil.of(startTime));
        deviceDataPageReqDto.setEndDate(ObjectUtil.isEmpty(endTime) ? null : LocalDateTimeUtil.of(endTime));

        Page<DeviceDataVo> page = deviceDataMapper.selectByPage(deviceDataPageReqDto);

        return PageResponse.of(page, DeviceDataVo.class);
    }

    /**
     * 按日查询设备数据
     *
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        //统计有数据的时间段的数据
        List<DeviceDataGraphVo> list = deviceDataMapper.queryDeviceDataListByDay(iotId, functionId, LocalDateTimeUtil.of(startTime), LocalDateTimeUtil.of(endTime));

        //生成当前一天所有时段的数据
        List<DeviceDataGraphVo> dayList = DeviceDataGraphVo.dayInstance(LocalDateTimeUtil.of(startTime));

        //将传到的数据转换为map结构{{00:00:10},{01:00:20},{05:00:10}}
        Map<String, Double> map = list.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));

        //使用map覆盖对应时间的数据
        dayList.stream().forEach(deviceDataGraphVo -> {
            Double v = map.get(deviceDataGraphVo.getDateTime());
            if (ObjectUtil.isNotEmpty(v)) {
                deviceDataGraphVo.setDataValue(v);
            }
        });

        return dayList;
    }

    /**
     * 按周查询设备数据
     *
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        List<DeviceDataGraphVo> list = deviceDataMapper.queryDeviceDataListByWeek(iotId, functionId, LocalDateTimeUtil.of(startTime), LocalDateTimeUtil.of(endTime));

        List<DeviceDataGraphVo> weekList = DeviceDataGraphVo.weekInstance(LocalDateTimeUtil.of(startTime));

        Map<String, Double> map = list.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        weekList.stream().forEach(deviceDataGraphVo -> {
            Double v = map.get(deviceDataGraphVo.getDateTime());
            if (ObjectUtil.isNotEmpty(v)) {
                deviceDataGraphVo.setDataValue(v);
            }
        });

        return weekList;
    }
}
